
function output = Bcr4bp_Rot2Iner3D(tt , xx , pc , coord , aux)
%
% BCR4BP坐标转换，Rot -> Inertial，3D
%
% 备注：
% case1: EMRot
% input: xx_b1c_rot
% output: xx_ec_ini, xx_mc_ini
%
% case2: SB1Rot
% input: xx_b2c_rot
% output: xx_ec_ini, xx_mc_ini
%
% 时间：2024年06月03日
% 作者：张晨
% 单位：CSU
% 邮箱：chenzhang@csu.ac.cn
%%%%%%%%%%%%%%%%%%%%%%%

xx = xx(:);

mu = aux.EMRot.mu;
mus = aux.EMRot.mus;
as = aux.EMRot.as;
wm = aux.SB1Rot.wm;

if strcmp(coord , 'emrot')

    xx_b1c_rot = xx;

    % 计算旋转矩阵
    alpha = tt;
    dalpha = 1;
    C = [cos(alpha) , -sin(alpha) , 0;
        sin(alpha) , cos(alpha) , 0;
        0 , 0 , 1];
    dC = dalpha * [-sin(alpha) , -cos(alpha) , 0;
        cos(alpha) , -sin(alpha) , 0;
        0 , 0 , 0];
    TMtx = [C , zeros(3); dC , C];

    % 先Rot相对P1/P2，再转
    xx_b1e_rot = [-mu ; 0 ; 0 ; 0 ; 0 ; 0];
    xx_b1m_rot = [(1 - mu) ; 0 ; 0 ; 0 ; 0 ; 0];

    if strcmp(pc , 'p1')

        xx_ec_rot = xx_b1c_rot - xx_b1e_rot;
        xx_ec_ini = TMtx * xx_ec_rot;

        output = xx_ec_ini';

    elseif strcmp(pc , 'p2')

        xx_mc_rot = xx_b1c_rot - xx_b1m_rot;
        xx_mc_ini = TMtx * xx_mc_rot;

        output = xx_mc_ini';

    else

        fprintf('wrong!')
        xx = [];
        return;

    end

elseif strcmp(coord , 'sb1rot')

    xx_b2c_rot = xx;

    % 计算旋转矩阵
    alpha = tt;
    dalpha = 1;
    C = [cos(alpha) , -sin(alpha) , 0;
        sin(alpha) , cos(alpha) , 0;
        0 , 0 , 1];
    dC = dalpha * [-sin(alpha) , -cos(alpha) , 0;
        cos(alpha) , -sin(alpha) , 0;
        0 , 0 , 0];
    TMtx = [C , zeros(3); dC , C];

    % 计算E-M相对S-B1的转角thetam
    thetam = aux.SB1Rot.wm * tt;
    xx_b2b1_rot = [mus / (mus + 1) ; 0 ; 0 ; 0 ; 0 ; 0];
    xx_b1e_rot = [- 1 / as * mu * cos(thetam); - 1 / as * mu * sin(thetam) ; 0 ;...
        wm * mu / as * sin(thetam) ; -wm / as * mu * cos(thetam) ; 0];
    xx_b1m_rot = [1 / as * (1 - mu) * cos(thetam); 1 / as * (1 - mu) * sin(thetam) ; 0 ;...
        -wm / as * (1 - mu) * sin(thetam) ; wm / as * (1 - mu) * cos(thetam) ; 0];

    if strcmp(pc , 'p1')

        xx_b2e_rot = xx_b2b1_rot + xx_b1e_rot;
        xx_ec_rot = xx_b2c_rot - xx_b2e_rot;
        xx_ec_ini = TMtx * xx_ec_rot;

        output = xx_ec_ini';

    elseif strcmp(pc , 'p2')

        xx_b2m_rot = xx_b2b1_rot + xx_b1m_rot;
        xx_mc_rot = xx_b2c_rot - xx_b2m_rot;
        XX_mc_ini = TMtx * xx_mc_rot;

        output = XX_mc_ini';

    else

        fprintf('wrong!')
        xx = [];
        return;

    end

    dd = [];

end

end
